package chen.bupt.spray

import akka.actor.ActorSystem
import spray.caching.{LruCache, Cache}
import spray.util._

import scala.concurrent.Future

/**
 * Created with IntelliJ IDEA.
 * User: chenlingpeng
 * Date: 2014/10/14
 * Time: 11:28
 *
//                            _ooOoo_
//                           o8888888o
//                           88" . "88
//                           (| -_- |)
//                            O\ = /O
//                        ____/`---'\____
//                      .   ' \\| |// `.
//                       / \\||| : |||// \
//                     / _||||| -:- |||||- \
//                       | | \\\ - /// | |
//                     | \_| ''\---/'' | |
//                      \ .-\__ `-` ___/-. /
//                   ___`. .' /--.--\ `. . __
//                ."" '< `.___\_<|>_/___.' >'"".
//               | | : `- \`.;`\ _ /`;.`/ - ` : | |
//                 \ \ `-. \_ __\ /__ _/ .-` / /
//         ======`-.____`-.___\_____/___.-`____.-'======
//                            `=---='
//
//         .............................................
//
//   █████▒█    ██  ▄████▄   ██ ▄█▀       ██████╗ ██╗   ██╗ ██████╗
// ▓██   ▒ ██  ▓██▒▒██▀ ▀█   ██▄█▒        ██╔══██╗██║   ██║██╔════╝
// ▒████ ░▓██  ▒██░▒▓█    ▄ ▓███▄░        ██████╔╝██║   ██║██║  ███╗
// ░▓█▒  ░▓▓█  ░██░▒▓▓▄ ▄██▒▓██ █▄        ██╔══██╗██║   ██║██║   ██║
// ░▒█░   ▒▒█████▓ ▒ ▓███▀ ░▒██▒ █▄       ██████╔╝╚██████╔╝╚██████╔╝
//  ▒ ░   ░▒▓▒ ▒ ▒ ░ ░▒ ▒  ░▒ ▒▒ ▓▒       ╚═════╝  ╚═════╝  ╚═════╝
//  ░     ░░▒░ ░ ░   ░  ▒   ░ ░▒ ▒░
//  ░ ░    ░░░ ░ ░ ░        ░ ░░ ░
//           ░     ░ ░      ░  ░
// 
 *
 */
object SprayCachingTest extends App {
  val system = ActorSystem()
  import system.dispatcher

  def expensiveOp(): Double = {
    println("1")
    new util.Random().nextDouble()
  }

  val cache: Cache[Double] = LruCache(maxCapacity=2, initialCapacity=1)

  def cachedOp[T](key:T) :Future[Double] = cache(key){
    expensiveOp()
  }

  cachedOp("foo").await == cachedOp("foo").await
  cachedOp("bar").await != cachedOp("foo").await
  cachedOp("foo1").await == cachedOp("foo1").await
  cachedOp("bar2").await != cachedOp("foo2").await
  cachedOp("foo").await == cachedOp("foo").await

  system.shutdown()
}
